<template>
  <div class="wechat-list">
    <h2>微信列表-new24</h2>
    <div class="contact-list">
      <div v-for="contact in contacts" :key="contact.id"  @click="handleContactClick(contact)">
					 <div  class="contact-item"  v-if="userInfo.id != contact.id">
						 <div class="avatar">{{ contact.name.charAt(0) }}</div>
						 <div class="info">
						   <h3>{{ contact.name }}</h3>
						   <p>{{ contact.lastMessage }}</p>
						 </div>
					 </div>
        
      </div>
    </div>
  </div>
</template>

<script>
import { storage } from '../../utils/storage.js';
export default {
  data() {
    return {
      contacts: [
        { id: 'user_00', name: '张三', type: 'private', lastMessage: '你好，最近怎么样？' },
        { id: 'user_01', name: '马超', type: 'private', lastMessage: '你好，最近怎么样？' },
        { id: 'user_02', name: '李四', type: 'private', lastMessage: '会议资料已发送' },
        { id: 'user_03', name: '王五', type: 'private', lastMessage: '会议资料已发送' },
        { id: 'user_04', name: '六四', type: 'private', lastMessage: '会议资料已发送' },
        { id: 'user_05', name: '王超', type: 'private', lastMessage: '会议资料已发送' },
        { id: 'group_001', name: '项目组', type: 'group', lastMessage: '王五: 进度更新了' },
        { id: 'group_002', name: '家人群', type: 'group', lastMessage: '妈妈: 周末回家吗？' },
      ],
			userInfo:{}
    }
  },
	mounted() {
		// this.userInfo={id:'user_00',uid: "1910260985059344386",name:'sandy'}
		// localStorage.setItem('userInfo',JSON.stringify(this.userInfo))
		const randomNumbers=Math.floor(Math.random() * 5) + 1
		console.log(randomNumbers); // 例如: ["03", "00", "07", "09", "01"]
		const id = 'user_0'+randomNumbers
		let name
		const index = this.contacts.findIndex(item=>item.id==id)
		if(index>0){
			name = this.contacts[index].name
		}
		this.userInfo={id:id,name:name}
		storage.setItem('userInfo',this.userInfo)
		this.userInfo = storage.getItem('userInfo') ? (storage.getItem('userInfo')) : {},
		console.log('userInfo',this.userInfo )
	},
  methods: {
    handleContactClick(contact) {
      if (contact.type === 'private') {
        uni.navigateTo({
          url: '/pages/WeChat/PrivateChat?userId='+contact.id+'&userName='+contact.name
        })
      } else {
        uni.navigateTo({
          url: '/pages/WeChat/GroupChat?groupId='+contact.id+'&groupName='+contact.name, 
        })
      }
    }
  }
}
</script>

<style scoped>
.wechat-list {
  padding: 20px;
}
.contact-list {
  margin-top: 20px;
}
.contact-item {
  display: flex;
  padding: 10px;
  border-bottom: 1px solid #eee;
  cursor: pointer;
}
.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #07C160;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;
}
.info h3 {
  margin: 0;
  font-size: 16px;
}
.info p {
  margin: 5px 0 0;
  font-size: 12px;
  color: #888;
}
</style>